<?php

namespace app\common\model\campus;

use app\common\model\BaseModel;
use app\common\model\Region as RegionModel;

/**
 * 校区模型
 * Class Shop
 * @package app\common\model\store
 */
class Campus extends BaseModel
{
    protected $name = 'campus';

    /**
     * 追加字段
     * @var array
     */
    protected $append = ['region'];

    /**
     * 关联文章封面图
     * @return \think\model\relation\HasOne
     */
    public function logo()
    {
        $module = self::getCalledModule() ?: 'common';
        return $this->hasOne("app\\{$module}\\model\\UploadFile", 'file_id', 'logo_image_id');
    }

    /**
     * 地区名称
     * @param $value
     * @param $data
     * @return array
     */
    public function getRegionAttr($value, $data)
    {
        return [
            'province' => RegionModel::getNameById($data['province_id']),
            'city' => RegionModel::getNameById($data['city_id']),
            'region' => $data['region_id'] == 0 ? '' : RegionModel::getNameById($data['region_id']),
        ];
    }


    /**
     * 关联商品图片表
     * @return \think\model\relation\HasMany
     */
    public function image()
    {
        return $this->hasMany('CampusImage')->order(['id' => 'asc']);
    }

    /**
     * 校区详情
     * @param $campus_id
     * @return static
     */
    public static function detail($campus_id)
    {
        /* @var $model self */
        $model = (new static)->with([
            'image.file',
        ])->where('campus_id', '=', $campus_id)
            ->find();
        if (empty($model)) {
            return $model;
        }
        // 整理商品数据并返回
        return $model;
    }

}